﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Markup;
using DAL;

namespace SS.WPFusercontrol
{
    /// <summary>
    /// Interaction logic for Customer.xaml
    /// </summary>
    public partial class Customer : UserControl
    {
        public Customer()
        {
            InitializeComponent();
            ErrorLabel.Visibility = Visibility.Hidden;
        }
        private void QueryButton_Click(object sender, RoutedEventArgs e)
        {
            ErrorLabel.Visibility = Visibility.Hidden;
            Customers customers = new Customers();
            DataTable DT = customers.GetCustomers(NameTextbox.Text.Trim());
            DT.DefaultView.AllowNew = false;
            dataGrid1.ItemsSource = DT.DefaultView;
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            if (NameTextbox.Text.Trim() == string.Empty)
            {
                ErrorLabel.Content = "产品名称不能为空，请重新输入！";
                ErrorLabel.Foreground = Brushes.Red;
            }
            else
            {
                Customers customers = new Customers();
                if (customers.CheckCustomers(NameTextbox.Text.Trim()))
                {
                    ErrorLabel.Content = "产品名称已存在，请确认产品信息！";
                    ErrorLabel.Foreground = Brushes.Red;
                }
                else
                {
                    if (customers.SaveCustomers(NameTextbox.Text.Trim(), PhoneNumberTextBox.Text.Trim(), DesriptionTextbox.Text.Trim()))
                    {
                        ErrorLabel.Content = "添加成功！";
                        ErrorLabel.Foreground = Brushes.Green;
                        DataTable DT = customers.GetCustomers(NameTextbox.Text.Trim());
                        DT.DefaultView.AllowNew = false;
                        dataGrid1.ItemsSource = DT.DefaultView;
                    }
                    else
                    {
                        ErrorLabel.Content = "添加失败，请重新操作！如仍然失败，请联系管理员！";
                        ErrorLabel.Foreground = Brushes.Red;
                    }
                }
            }
            ErrorLabel.Visibility = Visibility.Visible;
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            if (NameTextbox.Text.Trim() == string.Empty)
            {
                ErrorLabel.Content = "请输入产品名称！";
                ErrorLabel.Foreground = Brushes.Red;
            }
            else
            {
                Customers customers = new Customers();
                if (customers.CheckCustomers(NameTextbox.Text.Trim()))
                {
                    if (customers.SaveCustomers(NameTextbox.Text.Trim(), PhoneNumberTextBox.Text.Trim(), DesriptionTextbox.Text.Trim()))
                    {
                        ErrorLabel.Content = "更新成功！";
                        ErrorLabel.Foreground = Brushes.Green;
                        DataTable DT = customers.GetCustomers(NameTextbox.Text.Trim());
                        DT.DefaultView.AllowNew = false;
                        dataGrid1.ItemsSource = DT.DefaultView;
                    }
                    else
                    {
                        ErrorLabel.Content = "更新失败，请重新操作！如仍然失败，请联系管理员！";
                        ErrorLabel.Foreground = Brushes.Red;
                    }
                }
                else
                {
                    ErrorLabel.Content = "产品名称不存在，请确认产品信息！";
                    ErrorLabel.Foreground = Brushes.Red;

                }
            }
            ErrorLabel.Visibility = Visibility.Visible;
        }

        private void SelectAllButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.Items.Count > 0)
            {
                CheckBox FirstCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(dataGrid1.Items[0]) as CheckBox;
                bool IsCheckedCheckBox = (bool)FirstCheckBoxInCell.IsChecked;
                foreach (var item in dataGrid1.Items)
                {
                    CheckBox selectCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(item) as CheckBox;
                    if (selectCheckBoxInCell != null)
                    {
                        selectCheckBoxInCell.IsChecked = !IsCheckedCheckBox;
                    }
                }

            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            string IDs = "";
            if (dataGrid1.Items.Count > 0)
            {
                foreach (DataRowView item in dataGrid1.Items)
                {
                    CheckBox selectCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(item) as CheckBox;
                    if (selectCheckBoxInCell.IsChecked == true)
                    {
                        IDs = IDs + item["CustomerID"].ToString() + ",";
                    }
                }
            }

            Customers customers = new Customers();
            if (customers.DeleteCustomers(IDs.TrimEnd(',')))
            {
                ErrorLabel.Content = "成功删掉！";
                ErrorLabel.Foreground = Brushes.Green;
                DataTable DT = customers.GetCustomers(NameTextbox.Text.Trim());
                DT.DefaultView.AllowNew = false;
                dataGrid1.ItemsSource = DT.DefaultView;
            }
            else
            {
                ErrorLabel.Content = "删掉失败，请重新操作！如仍然失败，请联系管理员！";
                ErrorLabel.Foreground = Brushes.Red;
            }
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            object item = dataGrid1.CurrentItem;
            if (item != null)
            {
                NameTextbox.Text = ((DataRowView)(item)).Row.ItemArray[1].ToString();
                PhoneNumberTextBox.Text = ((DataRowView)(item)).Row.ItemArray[2].ToString();
                DesriptionTextbox.Text = ((DataRowView)(item)).Row.ItemArray[3].ToString();
            }
        }
    }
}
